<?php
namespace app\admin\controller;

use think\Controller;
use think\Session;

/**
 * 精华分享文章控制器
 */
class Article extends Base
{
    /**
     * 列表
     * @return [type] [description]
     */
    public function lists()
    {
        $list = Db('article')->field('id,title,author,num,create_time,is_show,is_del')->where('is_del',0)->select();
        $this->assign('list',$list);

        return $this->fetch();
    }


    /**
     * 添加
     */
    public function add()
    {
        if(request()->isPost()){
            $data['title']          = input('title');
            $data['author']         = input('author');
            $data['content']        = input('content');
            $data['create_time']    = time();
            $data['update_time']    = time();
            $data['is_show']        = input('is_show');

            $re = Db('article')->insert($data);
            if($re){
                return $this->success('成功');
            }else{
                return $this->error('网络错误');
            }
        }

        return $this->fetch();
        
    }


    /**
     * 编辑
     * @return [type] [description]
     */
    public function edit()
    {
        $id = input('id');
        if(!$id){
            $this->error("参数不完整");
        }

        if(request()->isPost()){

            input('title')      == null || $data['title']       = input('title');
            input('author')     == null || $data['author']      = input('author');
            input('content')    == null || $data['content']     = input('content');
            input('is_show')    == null || $data['is_show']     = input('is_show');
            input('is_del')     == null || $data['is_del']      = input('is_del');

            if($data){
                $data['update_time'] = time();
            }

            // 更新数据库
            $re = Db('article')->where('id',$id)->update($data);
            if($re){
                return $this->success('成功');
            }else{
                return $this->error('网络错误');
            }
        }

        $info = Db('article')->where('id',$id)->find();
        $this->assign('info',$info);

        return $this->fetch();

    }




}
